<?php
/**
 * PECK 小鸡啄米, LD's toolkit
 *
 * @filesource pView.class.php
 * @package PECK
 * @subpackage _core
 * @version $id: 0.1, utf8, Sat Jan  9 11:17:11 CST 2010
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2010 MiFunny China Inc.
 * @link http://mifunny.info/
 * @see 
 */
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
class peck{
	
	/**
	 * 初始化小鸡啄米
	 * 
	 * @param array $policy
	 */
	public function __construct(array & $policy=null){
		if( file_exists(dirname(__FILE__).DS.'_policy.array.php') ){
			
			//规范目录...
			$panic = array('model_path','helper_path','controller_path','peck_path',);
			$Coldplay = get_include_path();
			foreach($panic as $k){
				$policy['sys'][$k] = rtrim($policy['sys'][$k], '\\/').DS;
				if( $k == 'peck_path'){
					$p = PATH_SEPARATOR.$policy['sys'][$k];
					$Coldplay .= $p.'_db'.DS  .$p.'_cache'.DS  .$p.'_helper'.DS .$p.'_view'.DS;
				}else 
					$Coldplay .= PATH_SEPARATOR.$policy['sys'][$k];
			}
            set_include_path($Coldplay); //echo get_include_path();

			$default = include('_policy.array.php');
			foreach($default as $k=>$v){
				if( !empty($policy[$k]) ){
					$v = array_merge($v, $policy[$k]);
				}
				_core::set($k,$v);
				unset($policy[$k]);
			} 
			// 添加用户自定义选项...
			if( count($policy) > 0 ) foreach ($policy as $k=>$v){
				_core::set($k,$v);
			}
			//unset($default);
			
			include_once 'global.func.php';
			_core::set('sys/microtime', _mtime());
		}else
			throw new LogicException('('.__METHOD__.')Error: Show Me the Meaning !');
	}
	
	public function __destruct(){
		//echo '<hr />start: ', _core::get('sys/microtime'), ' and end: ', _mtime();
	}
	
	/**
	 * 开始我们的MVC之旅
	 */
    public function start(){
		//初始化默认配置
    	$this->entry();
		
		//URL 调度
		$this->dispatcher();
    
	}//END func start
	
	/**
	 * 初始化默认配置
	 */
	public function entry(){
		$sys = _core::get('sys');

	    // 判断是否可以压缩输出
	    if ($sys['gzip'] === TRUE){
		    if (extension_loaded('zlib')){
                if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) 
                  AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE){
                ini_set('zlib.output_compression_level', $sys['gzip_level']);
                ob_start('ob_gzhandler');
			    }
		    }
        }
        
        // 禁用 magic quote
        @set_magic_quotes_runtime(0);

		// 处理被 magic quotes 自动转义过的数据
        if (get_magic_quotes_gpc()){
            $in = array(& $_GET, & $_POST, & $_COOKIE, & $_REQUEST);
            while (list ($k, $v) = each($in)){
                foreach ($v as $key => $val){
                    if (! is_array($val)){
                        $in[$k][$key] = stripslashes($val);
                        continue;
                    }
                    $in[] = & $in[$k][$key];
                }
            }
            unset($in);
        }

        // 输出编码 empty($sys['charset']) or header('Content-Type:text/html;charset='.$sys['charset']);
        
        // 设置默认的时区
        date_default_timezone_set(_core::get('sys/timezone'));
		
       // 启动SESSION ? if( $sys['session'] ) session_start();
	}//END func entry

	/**
	 * URL调度器
	 */
	public function dispatcher(){
		//标准方法...
		if( _core::get('url/method') == 'standard' ){
			$controller = _req('controller', 'default');
			$action = _req('action', 'index');
		}
		$controller_path = rtrim(_core::get('sys/controller_path'), '\\/').DS.$controller.'.php';
		if( file_exists($controller_path) ){
			require_once $controller_path;
			$controller = 'controller'.ucfirst($controller);
			$action = 'action'.ucfirst($action);
			$response = new $controller;
			$response->execute($action, array_merge($_GET, $_POST));
		}else{
			header('Status: 404 Not Found');
		}
			
	}//END func dispatcher

}//ENF PECK

//存储核心变量的一个对象
class _core{
	//核心对象数组
	private $_core = array();
	private static $instance;
	const VERSION = 0.1;  //当前版本
	
	//禁用一系列魔法函数
	private function __construct(){}
	private function __clone(){}
	
	/**
	 * 获得实例对象
	 * 
	 * @return self
	 */
	public static function object(){
		if( !(self::$instance instanceof self) ){
           self::$instance = new self();
		}
       	return self::$instance;
	}
	
	/**
	 * 获得配置值
	 * 
	 * @param string $key 例: 'database/username'
	 *
	 * @return mixed
	 */
	public static function get($key=FALSE){
		$core =& self::object()->_core;
		if( $key ){
			$keys = explode('/', $key);
			foreach($keys as $k){
				$core =& $core[$k];
			}
		}
		return $core;
	}
	
	/**
	 * 设置内部配置值
	 * 
	 * @param string $key 同get()
	 * @param mixed $value
	 */
	public static function set($key, $value){
		$core =& self::object()->_core;
		if( $key ){		
			$keys = explode('/', $key);
			foreach($keys as $k){
				$core =& $core[$k];
			}
		}//key not null
		$core = $value;
	}
}//END class _core
?>
